#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
struct ListNode {
    int val;
    ListNode* next;
    ListNode(int x) : val(x), next(NULL) {}
};
class Solution {
public:
    ListNode* reverseKGroup(ListNode* head, int k) {
        int n = k;
        if (n <= 1)return head;
        ListNode* p = head, * p1, * p2, * p3, * p4;
        p1 = p;
        while (k--)
        {
            if (p != NULL)p = p->next;
            else return head;
        }
        p2 = p;
        p3 = p1;
        p = p3->next;
        k = n;
        while (--k)
        {
            p4 = p->next;
            p->next = p3;
            p3 = p;
            p = p4;

        }
        p1->next = p2;
        head = p3;
        while (1)
        {
            p = p1->next;
            k = n;
            while (k--)
            {
                if (p != NULL)p = p->next;
                else return head;
            }
            p2 = p;
            p3 = p1->next;
            p = p3->next;
            p3->next = p2;
            k = n;
            while (--k)
            {
                p4 = p->next;
                p->next = p3;
                p3 = p;
                p = p4;

            }
            p = p1->next;
            p1->next = p3;
            p1 = p;
        }
    }
};
int main()
{
    Solution sol;
}